package com.ldzy.controller;

import com.ldzy.pojo.User;
import com.ldzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/")
    public String hello() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request) {
        try {
            User user1 = userService.login(user);
            if (user1 != null) {
                request.getSession().setAttribute("user_session", user1);
                return "redirect:/admin/main.jsp";
            }
            request.setAttribute("msg", "用户名或密码错误");
            return "forward:/admin/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "系统错误");
            return "forward:/admin/login.jsp";
        }
    }
    @RequestMapping("/to")
    public String to(){
        return "main";
    }
    @RequestMapping("/logout")
    public String lognout(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.invalidate();
        return "redirect:/admin/login.jsp";
    }
}